package com.shoujifeng.snowmusic.player;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.http.ServerAccess;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 意见反馈
 * @author luo
 *
 */
public class IdeaActivity extends Activity{
	private Button breakBtn;
	private Button comButton;
	private EditText contentEditText;
	private EditText emailEditText;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.idea_lay);
		init();
	}
	private void init(){
		breakBtn = (Button) findViewById(R.id.breakBtn);
		contentEditText = (EditText) findViewById(R.id.idea_content);
		emailEditText = (EditText) findViewById(R.id.idea_mail);
		comButton = (Button) findViewById(R.id.idea_okBtn);
		breakBtn.setOnClickListener(onClickListener);
		comButton.setOnClickListener(onClickListener);
	}
	
	private View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.breakBtn:{
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.right_out);
			}
				break;
			case R.id.idea_okBtn:
			{
				String content = contentEditText.getText().toString();
				String mail = emailEditText.getText().toString();
				
				if(content.length() < 5) {
					Toast.makeText(IdeaActivity.this, "内容不能少于5个字.", Toast.LENGTH_SHORT).show();
					return;
				}
				
				if(mail.length() < 5) {
					Toast.makeText(IdeaActivity.this, "请填写邮箱.", Toast.LENGTH_SHORT).show();
					return;
				}
				
				Toast.makeText(IdeaActivity.this, "正在提交意见...", Toast.LENGTH_SHORT).show();
				sendIdea(content, mail);
				
			}
			default:
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	private void sendIdea(final String content, final String mail) {
		new AsyncTask<Integer, Integer, Integer>() {

			@Override
			protected Integer doInBackground(Integer... params) {
				try {
					ServerAccess serverAccess = new ServerAccess(IdeaActivity.this);
					serverAccess.addIder(content, mail);
					
					publishProgress(1);
				} catch (Exception e) {
					publishProgress(0);
				}
				return null;
			}

			@Override
			protected void onProgressUpdate(Integer... values) {
				switch (values[0]) {
				case 0:
				{
					Toast.makeText(IdeaActivity.this, "提交失败, 请检查网络！", Toast.LENGTH_SHORT).show();
				}
					break;
				case 1:
				{
					Toast.makeText(IdeaActivity.this, "提交成功, 谢谢您的宝贵意见!", Toast.LENGTH_SHORT).show();
				}
					break;
				default:
					break;
				}
				
				finish();
				super.onProgressUpdate(values);
			}
			
			
		}.execute(0);
	}
}

